package com.hpradip.android.accessNumberSpeedDialer;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

public class TelephoneUtils {

	public static void dialNumber(Context context, int contactId, String number,
			String accessNumber, String passcode, String isdCode, String pauseCharacter ,GoogleAnalyticsTracker tracker) {
		Intent intent = new Intent(Intent.ACTION_CALL);
		boolean updateContacted = false;
		
		if (number==null || number.length() == 0)
			return;
		
		String uri = "tel:";

		number = number.replace("-", "");

		number = number.replace(" ", "");

		if (number.length() > 10) {

			if (!(accessNumber != null && accessNumber.trim().length() > 0)) {
				Toast.makeText(context, R.string.msg_empty_access_number,
						Toast.LENGTH_SHORT).show();
				return;
			} else {
				try {
					Long.parseLong(accessNumber);
				} catch (NumberFormatException ex) {
					Toast.makeText(context, R.string.msg_invalid_access_number,
							Toast.LENGTH_SHORT).show();
					return;
				}
			}

			if (!(passcode != null && passcode.trim().length() > 0)) {
				Toast.makeText(context, R.string.msg_empty_passcode,
						Toast.LENGTH_SHORT).show();
				return;
			} else {
				try {
					Long.parseLong(passcode);
				} catch (NumberFormatException ex) {
					Toast.makeText(context, R.string.msg_invalid_passcode,
							Toast.LENGTH_SHORT).show();
					return;
				}
			}

			if (!(isdCode != null && isdCode.trim().length() > 0)) {
				Toast.makeText(context, R.string.msg_empty_isd_code,
						Toast.LENGTH_SHORT).show();
				return;
			} else {
				try {
					Long.parseLong(isdCode);
				} catch (NumberFormatException ex) {
					Toast.makeText(context, R.string.msg_invalid_isd_code,
							Toast.LENGTH_SHORT).show();
					return;
				}
			}

			if (accessNumber != null){
				updateContacted = true;
				uri += accessNumber + pauseCharacter;
			}

			if (passcode != null)
				uri += passcode;

			String countryCode = number.substring(0, number.length() - 10);

			String prefix = "+";
			if (countryCode.startsWith(prefix))
				countryCode = countryCode.replace(prefix, "");

			prefix = "0";
			if (countryCode.startsWith(prefix))
				countryCode = countryCode.replace(prefix, "");

			number = isdCode + countryCode
					+ number.substring(number.length() - 10, number.length());
			Log.d(AccessNumberSpeedDialerActivity.TAG, "Dialing number: " + number);
			
			 tracker.trackEvent(
			            "UserActions",  // Category
			            "Call",  // Action
			            "international", // Label
			            1);       // Value

		}
		uri += number + "#";
		intent.setData(Uri.parse(uri));
		context.startActivity(intent);
		if (updateContacted && contactId>0)
			ContactsProvider.UpdateTimesContacted(context, contactId);
	}
}
